package com.itheima.web.controller.system;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.system.Dept;
import com.itheima.domain.system.User;
import com.itheima.service.system.DeptService;
import com.itheima.service.system.Impl.DeptServiceImpl;
import com.itheima.service.system.UserService;
import com.itheima.service.system.Impl.UserServiceImpl;
import com.itheima.utils.BeanUtil;
import com.itheima.utils.MD5Util;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/system/user")
public class UserServlet extends HttpServlet {
    UserService userService=new UserServiceImpl();
    DeptService deptService=new DeptServiceImpl();
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

            //设置请求和响应的编码    在filter 过滤器中写
//        req.setCharacterEncoding("UTF-8");
//        resp.setContentType("text/html;charset=UTF-8");

            String operation = req.getParameter("operation");
            if ("list".equals(operation)){
                findAll(req, resp);
            } else if ("toAdd".equals(operation)) {
                List<Dept> userList = deptService.findAll();
                req.setAttribute("userList",userList);
                req.getRequestDispatcher("/WEB-INF/pages/system/user/add.jsp").forward(req,resp);
            } else if ("save".equals(operation)) {
                //   Map<String, String[]> parameterMap = req.getParameterMap();
                User user = BeanUtil.fillBean(req, User.class,"yyyy-MM-dd");

                user.setPassword( MD5Util.md5(user.getPassword()));
                userService.save(user);
                resp.sendRedirect(req.getContextPath()+"/system/user?operation=list");
            } else if ("delete".equals(operation)) {
                String id = req.getParameter("id");
                userService.delete(id);
                resp.sendRedirect(req.getContextPath()+"/system/user?operation=list");
            } else if ("toEdit".equals(operation)) {
                String id = req.getParameter("id");
                List<Dept> deptList = deptService.findAll();
                User user = userService.findById(id);
                req.setAttribute("deptList",deptList);
                req.setAttribute("user",user);
                req.getRequestDispatcher(req.getContextPath()+"/WEB-INF/pages/system/user/update.jsp").forward(req,resp);
            } else if ("edit".equals(operation)) {
                User user = BeanUtil.fillBean(req, User.class,"yyyy-MM-dd");
                userService.update(user);
                resp.sendRedirect(req.getContextPath()+"/system/user?operation=list");
            }
    }

        private void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            int page=1;
            int size=5;
            String page1 = req.getParameter("page");
            String size1 = req.getParameter("size");
            if (StringUtils.isNotBlank(page1)) {
                page = Integer.parseInt(page1);
            }
            if (StringUtils.isNotBlank(size1)) {
                size=Integer.parseInt(size1);
            }
            List<User> all = userService.findAll(page, size);
            PageInfo<User> userPageInfo = new PageInfo<>(all);
            req.setAttribute("page",userPageInfo);
            //转发 web-inf 前要加"/"
            req.getRequestDispatcher("/WEB-INF/pages/system/user/list.jsp").forward(req,resp);
        }
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
